<%#
 Copyright 2008 Steven Barth <steven@midlink.org>
 Copyright 2008-2016 Jo-Philipp Wich <jow@openwrt.org>
 Copyright 2012 David Menting <david@nut-bolt.nl>
 Licensed to the public under the Apache License 2.0.
-%>

<%
	local sys  = require "luci.sys"
	local util = require "luci.util"
	local http = require "luci.http"
	local disp = require "luci.dispatcher"

	local boardinfo = util.ubus("system", "board")

	local request  = disp.context.path
	local request2 = disp.context.request

	local category = request[1]
	local cattree  = category and disp.node(category)

	local leaf = request2[#request2]

	local tree = disp.node()
	local node = disp.context.dispatched

	local categories = disp.node_childs(tree)

	local c = tree
	local i, r

	-- tag all nodes leading to this page
	for i, r in ipairs(request) do
		if c.nodes and c.nodes[r] then
			c = c.nodes[r]
			c._menu_selected = true
		end
	end

	-- send as HTML5
	http.prepare_content("text/html")

	local function nodeurl(prefix, name, query)
		local u = url(prefix, name)
		if query then
			u = u .. http.build_querystring(query)
		end
		return pcdata(u)
	end

	local function render_tabmenu(prefix, node, level)
		if not level then
			level = 1
		end

		local childs = disp.node_childs(node)
		if #childs > 0 then
			if level > 2 then
				write('<ul class="tabs">')
			end

			local selected_node
			local selected_name
			local i, v

			for i, v in ipairs(childs) do
				local nnode = node.nodes[v]
				if nnode._menu_selected then
					selected_node = nnode
					selected_name = v
				end

				if level > 2 then
					write('<li class="tabmenu-item-%s %s"><a href="%s">%s</a></li>' %{
						v, (nnode._menu_selected or (node.leaf and v == leaf)) and 'active' or '',
						nodeurl(prefix, v, nnode.query),
						striptags(translate(nnode.title))
					})
				end
			end

			if level > 2 then
				write('</ul>')
			end

			if selected_node then
				render_tabmenu(prefix .. "/" .. selected_name, selected_node, level + 1)
			end
		end
	end

	-- 渲染二级菜单
	local function render_submenu(prefix, node)
		local childs = disp.node_childs(node)
		if #childs > 0 then
			write('<ul>')

			for i, r in ipairs(childs) do
				local nnode = node.nodes[r]
				write('<li class="%s"><a href="%s">%s</a></li>\n' %{
					nnode._menu_selected and 'active open' or ' ',
					nodeurl(prefix, r, nnode.query),
					pcdata(striptags(translate(nnode.title)))

				})
			end

			write('</ul>')
		end
	end

	-- 渲染面包屑(导航栏)
	local function render_submenu_mini(prefix, node, phref)
		local childs = disp.node_childs(node)
		if #childs > 0 then
			

			for i, r in ipairs(childs) do
				local nnode = node.nodes[r]
				if nnode._menu_selected == true then 
					write('<a href="%s" title="%s">%s</a>\n' % {
						pcdata(phref),
						pcdata(striptags(translate(node.title))),
						pcdata(striptags(translate(node.title)))
					})
					write('<a href="%s" title="%s">%s</a>\n' % {
						nodeurl(prefix, r, nnode.query),
						pcdata(striptags(translate(nnode.title))),
						pcdata(striptags(translate(nnode.title)))
					})
				end
			end

			
		end
	end

	-- 渲染菜单入口
	local function render_topmenu(style)
		local childs = disp.node_childs(cattree)
		if #childs > 0 then
			
			for i, r in ipairs(childs) do
				local nnode = cattree.nodes[r]
				local grandchildren = disp.node_childs(nnode)
				-- if nnode._menu_selected then
				-- 	write("true " .. r)
				-- else
				-- 	write("false " .. r)
				-- end
				if #grandchildren > 0 and style == "normal" then
					-- add by blood
					-- icon selected
					local icorn_class = ""
					if r == "system" then
						icorn_class = "icon-cog"
					elseif r == "services" then
						icorn_class = "icon-user"
					elseif r == "network" then
						icorn_class = "icon-sitemap"
					elseif r == "apmgn" then
						icorn_class = "icon-glass"
					else
						icorn_class = "icon-th-list"
					end

					write('<li class="submenu %s">\n<a href="#">\n<i class="icon %s"> </i>\n<span>%s</span></a>\n' % {
						nnode._menu_selected and 'active open' or' ',
						icorn_class,
						pcdata(striptags(translate(nnode.title))) 
					})
					render_submenu(category .. "/" .. r, nnode)
					write('</li>\n')

				elseif #grandchildren > 0 and style == "mini" then
					render_submenu_mini(category .. "/" .. r, nnode, nodeurl(category, r, nnode.query))

				elseif pcdata(striptags(translate(nnode.title))) ~= pcdata(striptags(translate("Logout"))) then
					write('<li class="%s"><a href="%s">%s</a></li>' %{
						nnode._menu_selected and 'active open' or '',
						nodeurl(category, r, nnode.query),
						pcdata(striptags(translate(nnode.title)))
					})
				end
			end

		end
	end

	-- 保存变更入口
	local function render_changes()
		-- calculate the number of unsaved changes
		if tree.nodes[category] and tree.nodes[category].ucidata then
			local ucichanges = 0

			for i, j in pairs(require("luci.model.uci").cursor():changes()) do
				for k, l in pairs(j) do
					for m, n in pairs(l) do
						ucichanges = ucichanges + 1;
					end
				end
			end

			if ucichanges > 0 then
				
			
				write('<li class=""><a href="%s?redir=%s"><i class="icon-warning-sign"></i><span class="text badge badge-warning">%s: %d</span></a></li>' % {
					url(category, 'uci/changes'),
					http.urlencode(http.formvalue('redir') or REQUEST_URI),
					translate('Unsaved Changes'),
					ucichanges
				})
			end
		end
	end
-%>
<!DOCTYPE html>
<html lang="<%=luci.i18n.context.lang%>">
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<title><%=striptags( (boardinfo.model or "?") .. ( (node and node.title) and ' - ' .. translate(node.title) or '')) %></title>
		<!--[if lt IE 9]><script src="<%=media%>/html5.js?v=git-18.038.25564-8fe5d3d"></script><![endif]-->
		<meta name="viewport" content="initial-scale=1.0">
		<link rel="stylesheet" href="<%=media%>/cascade.css?v=git-18.038.25564-8fe5d3d">
		<link rel="stylesheet" media="only screen and (max-device-width: 854px)" href="<%=media%>/mobile.css?v=git-18.038.25564-8fe5d3d" type="text/css" />
		<link rel="shortcut icon" href="<%=media%>/favicon.ico">
		<% if node and node.css then %>
			<link rel="stylesheet" href="<%=resource%>/<%=node.css%>">
		<% end -%>
		<% if css then %>
			<style title="text/css"><%= css %></style>
		<% end -%>
		<script src="<%=resource%>/xhr.js?v=git-18.038.25564-8fe5d3d"></script>
		<link rel="stylesheet" href="<%=media%>/js/iview.css?v=git-18.038.25564-8fe5d3d" />
		<link rel="stylesheet" href="<%=media%>/css/bootstrap.min.css?v=git-18.038.25564-8fe5d3d" />
		<link rel="stylesheet" href="<%=media%>/css/bootstrap-responsive.min.css?v=git-18.038.25564-8fe5d3d" />
		<link rel="stylesheet" href="<%=media%>/css/fullcalendar.css?v=git-18.038.25564-8fe5d3d" />
		<link rel="stylesheet" href="<%=media%>/css/matrix-style.css?v=git-18.038.25564-8fe5d3d" />
		<link rel="stylesheet" href="<%=media%>/css/matrix-media.css?v=git-18.038.25564-8fe5d3d" />
		<link href="<%=media%>/font-awesome/css/font-awesome.css?v=git-18.038.25564-8fe5d3d" rel="stylesheet" />
		<link rel="stylesheet" href="<%=media%>/nsdef.css?v=git-18.038.25564-8fe5d3d" />

		<script src="<%=media%>/js/jquery.min.js?v=git-17.363.12130-557c884"></script>
		<script src="<%=media%>/js/bootstrap.min.js?v=git-17.363.12130-557c884"></script>
		<script src="<%=media%>/js/jquery.dataTables.min.js?v=git-17.363.12130-557c884"></script>
		<script src="<%=media%>/js/matrix.js?v=git-17.363.12130-557c884"></script>
	</head>

	<body class="lang_<%=luci.i18n.context.lang%> <%- if node then %><%= striptags( node.title ) %><%- end %>">
		<div id="header">
			<h1><a href="dashboard.html">Device</a></h1>
		</div>

		<!--top-Header-menu-->
		<div id="user-nav" class="navbar navbar-inverse">
	  		<ul class="nav">
	  			<% render_changes() %>
	  			<li clas="">
					<a title="" href="#" id="xhr_poll_status" style="display:none" onclick="XHR.running() ? XHR.halt() : XHR.run()">
						<i class="icon-refresh"></i>
						<span class="text badge badge-success" id="xhr_poll_status_on"><%:Auto Refresh%> <%:on%></span>
						<span class="text badge " id="xhr_poll_status_off" style="display:none"><%:Auto Refresh%> <%:off%></span>
					</a>
				</li>
			</ul>
		</div>

		<!--top-Header-menu of logout -->
		<div id="search" class="navbar navbar-inverse">
			<a title="" href="<%=controller%>/<%=category%>/logout" class="badge badge-info"><i class="icon icon-share-alt"></i> <span class="text "><%:Logout%></span></a>
		</div>

		<!--  -->
		<div id="sidebar">
			<a href="#" class="visible-phone"><i class="icon icon-home"></i> <%=boardinfo.model or "?"%></a>
			<ul>
				<% render_topmenu("normal")  %>
			</ul>
		</div>

		<!-- main content -->
		<div id="content">
			<div id="content-header">
				<div id="breadcrumb">
					<a href="<%=controller%>/<%=category%>/status/overview" title="Go to Home" class="tip-bottom"><i class="icon-home"></i> Home</a>
					<% render_topmenu("mini")  %>
				</div>
			</div>


			<div class="container-fluid">
				<div class="row-fluid">
					<div class="span12">
			<% if category then render_tabmenu(category, cattree) end %>
